package com.lt.bookkeeping.Utils;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.AssetManager;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Build;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.Display;
import android.widget.TextView;

import androidx.annotation.RequiresApi;

import com.lt.bookkeeping.ApplicationUtils;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 底部导航栏
 * @author LiuTong
 */
public class Utils {
    public static final int NO_COLOR = Color.TRANSPARENT;

    private Utils() {
    }

    /**
     * @param context used to get system services
     * @return screenWidth in pixels
     */
    public static int getScreenWidth(Context context) {
        DisplayMetrics metrics = context.getResources().getDisplayMetrics();
        return metrics.widthPixels;
    }

    public static int getScreenHeight(Context context) {
        DisplayMetrics metrics = context.getResources().getDisplayMetrics();
        return metrics.heightPixels;
    }
    @RequiresApi(api = Build.VERSION_CODES.R)
    public static Display getDisplay(Context context){
        return context.getDisplay();
    }

    @RequiresApi(api = Build.VERSION_CODES.R)
    public static int getRealScreenWidth(Context context) {
        DisplayMetrics metrics = context.getResources().getDisplayMetrics();
        getDisplay(context).getRealMetrics(metrics);
        return metrics.widthPixels;
    }

    @RequiresApi(api = Build.VERSION_CODES.R)
    public static int getScreenRealHeight(Context context) {
        DisplayMetrics metrics = context.getResources().getDisplayMetrics();
        getDisplay(context).getRealMetrics(metrics);
        return metrics.heightPixels;
    }


    /**
     * This method can be extended to get all android attributes color, string, dimension ...etc
     *
     * @param context          used to fetch android attribute
     * @param androidAttribute attribute codes like R.attr.colorAccent
     * @return in this case color of android attribute
     */
    public static int fetchContextColor(Context context, int androidAttribute) {
        TypedValue typedValue = new TypedValue();

        TypedArray a = context.obtainStyledAttributes(typedValue.data, new int[]{androidAttribute});
        int color = a.getColor(0, 0);

        a.recycle();

        return color;
    }

    /**
     * @param context used to fetch display metrics
     * @param dp      dp value
     * @return pixel value
     */
    public static int dp2px(Context context, float dp) {
        float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, context.getResources().getDisplayMetrics());
        return Math.round(px);
    }


    public static String getFormData(long currentTime){
        @SuppressLint("SimpleDateFormat")
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date(currentTime);
        return simpleDateFormat.format(date);

    }

    public static void setTextView(TextView tv,String imgString,int... dp )  {
        Context context = ApplicationUtils.getInstance().getContext();
        AssetManager assetManager = context.getResources().getAssets();
        int px = ScreenUtils.dip2px(context,15);
        try {
            if(dp != null && dp.length > 0){
                px = ScreenUtils.dip2px(context,dp[0]);
            }

            Typeface typeface = Typeface.createFromAsset(assetManager, "fonts/iconfont.ttf");
            tv.setText(imgString);

            tv.setTextSize(px);
            tv.setTypeface(typeface);

        }catch (Exception e){
            e.printStackTrace();
        }

    }
}
